﻿package bdz.projects.lavender.doi.ui
{
	import bdz.projects.lavender.doi.utils.DOIAudio;
	import flash.display.MovieClip;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.primitives.Plane;
	import flash.utils.*;
	import gs.*;
	import gs.easing.*;
	/**
	 * ...
	 * @author ...
	 */
	public class DOIIntroAnimations extends DisplayObject3D
	{
		
		private var _ease:Function = Strong.easeInOut;
		private var _speed:Number = .8;
		
		private var _frontmaterial:MovieMaterial;
		private var _backmaterial:MovieMaterial;
		
		private var _frontplane:Plane;
		private var _backplane:Plane;
		
		private var _frontframe:MovieClip;
		private var _backframe:MovieClip;
		
		private var _frontContainer:DisplayObject3D;
		private var _backContainer:DisplayObject3D;
		
		public var pcontainer:DisplayObject3D;
		
		private var _currentFront:MovieClip;
		private var _audio:DOIAudio;
		public function DOIIntroAnimations() {
			super("intro");
			initializeComponent();
		}
		private function initializeComponent():void {
			_frontContainer = new DisplayObject3D("_fronContainer");
			_backContainer = new DisplayObject3D("_backContainer");
			addChild(_backContainer);
			addChild(_frontContainer);
			
			_backContainer.rotationY = 180;
			_backContainer.z = 50;
			_frontframe = new IntroViewFrame();
			_backframe = new IntroViewFrame();
			
			_frontmaterial = new MovieMaterial(_frontframe, false, true, false);
			_frontframe.defmat = _frontmaterial;
			_frontmaterial.doubleSided = false;
			_frontmaterial.smooth = true;
			_backmaterial = new MovieMaterial(_backframe, false, true, false);
			_backframe.defmat = _backmaterial;
			_backmaterial.doubleSided = false;
			_backmaterial.smooth = true;
			
			_frontplane = new Plane(_frontmaterial, _frontframe.width, _frontframe.height, 6, 6);
			_backplane = new Plane(_backmaterial, _backframe.width, _backframe.height, 6, 6);
			
			_frontContainer.addChild(_frontplane, "front");
			_backContainer.addChild(_backplane, "back");
			
			_audio = DOIAudio.getInstance();
			
			y = 80;
			z = 0;
			scale = 0;
			rotationY = 0;
			TweenMax.to(this, 1.1, { scale:1,  ease:_ease, onStart:function() { _audio.playEffect(initplane); } } );
			
			_currentFront = _frontframe;
			_audio.playFlip();
		}
		private function flip():void {
			TweenMax.to(this, _speed, { rotationY:rotationY + 180, ease:_ease } );
			_audio.playFlip();
		}
		public function cleanUp():void {
			_frontframe = null;
			_backframe = null;
			
			_frontmaterial.destroy();
			_backmaterial.destroy();
			
			
			_frontContainer.removeChild(_frontplane);
			_frontContainer.removeChild(_backplane);
			pcontainer.removeChild(this);
			
		}
		public function moveToStandard():void {
			_audio.playFlip();
			TweenMax.to(this, 1.3, {scale:1, z:0, y:0, rotationY: 0, ease:_ease } );
		}
		public function createMovie(str:String):void {
			
			_currentFront.showAnimation(str);
		}
		
		
		public function kill():void {
			TweenMax.to(this, 1, { scale:0,  z:1000, y:150, rotationY: -180, ease:_ease, onComplete:cleanUp} );
		}
		
	}
	
}